package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.data.BcdString
import org.junit.Test
import org.slf4j.LoggerFactory
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousSocketChannel
import java.util.concurrent.TimeUnit

class DtenAioTest {
    @Test
    fun testHist1() {
        val addr = InetSocketAddress(HOST1, PORT)
        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(addr)
        f1[2, TimeUnit.SECONDS]
        if (f1.isCancelled) {
            lgr.error("fail to connect qdies server!")
        }

        val buf = ByteBuffer.wrap(BcdString.toByteArray(HEX_2))
        val f2 = sk.write(buf)
        lgr.info("sent: $HEX_2")
        f2[3, TimeUnit.SECONDS]
        buf.clear()
        val f3 = sk.read(buf)
        f3[5, TimeUnit.SECONDS]
        var hex = BcdString.hexString(ByteArray(buf.remaining()).also {
            buf.get(it)
        })
        lgr.info("got: ${hex}")

        // hex-3
        val buf2 = ByteBuffer.wrap(BcdString.toByteArray(HEX_3))
        val f4 = sk.write(buf2)
        lgr.info("sent: $HEX_3")
        f4[3, TimeUnit.SECONDS]
        buf.clear()
        val f5 = sk.read(buf)
        f5[5, TimeUnit.SECONDS]
        hex = BcdString.hexString(ByteArray(buf.remaining()).also {
            buf.get(it)
        })
        lgr.info("got: ${hex}")

        // hex-4
        val buf3 = ByteBuffer.wrap(BcdString.toByteArray(HEX_4))
        val f6 = sk.write(buf3)
        lgr.info("sent: $HEX_4")
        f6[3, TimeUnit.SECONDS]
        buf.clear()
        val f7 = sk.read(buf)
        f7[5, TimeUnit.SECONDS]
        hex = BcdString.hexString(ByteArray(buf.remaining()).also {
            buf.get(it)
        })
        lgr.info("got: ${hex}")

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()
    }

    @Test
    fun testHistMore() {
        val addr = InetSocketAddress(HOST2, PORT)
        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(addr)
        f1[2, TimeUnit.SECONDS]
        if (f1.isCancelled) {
            lgr.error("fail to connect qdies server!")
        }

        HEX_5.forEach {
            val buf = ByteBuffer.wrap(BcdString.toByteArray(it))
            val f2 = sk.write(buf)
            lgr.info("sent: $it")
            f2[3, TimeUnit.SECONDS]
            buf.clear()
            val f3 = sk.read(buf)
            f3[5, TimeUnit.SECONDS]
            var hex = BcdString.hexString(ByteArray(buf.remaining()).also {
                buf.get(it)
            })
            lgr.info("got: ${hex}")

            Thread.sleep(2 * 1000)
        }

        Thread.sleep(5 * 1000)

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()
    }

    @Test
    fun testHistPart() {
        val addr = InetSocketAddress(HOST2, PORT)
        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(addr)
        f1[2, TimeUnit.SECONDS]
        if (f1.isCancelled) {
            lgr.error("fail to connect qdies server!")
        }

        HEX_6.forEach {
            val buf = ByteBuffer.wrap(BcdString.toByteArray(it))
            val f2 = sk.write(buf)
            lgr.info("sent: $it")
            f2[3, TimeUnit.SECONDS]
            buf!!.clear()

            Thread.sleep(2 * 1000)
        }

        ByteBuffer.allocate(2048).also {
            val f3 = sk.read(it)
            f3[5, TimeUnit.SECONDS]
            it.flip()
            var hex = BcdString.hexString(ByteArray(it.remaining()).also { bs ->
                it.get(bs)
            })
            lgr.info("got: ${hex}")
        }

        Thread.sleep(5 * 1000)

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()
    }

    companion object {
        const val HOST1 = "localhost"
        const val HOST2 = "nb.intel-water.cn"
        const val PORT = 6436
        private val lgr = LoggerFactory.getLogger(DtenAioTest::class.java)
        const val HEX_1 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 20 0b 02 00 00 80
3f ae 16 45 20"""

        const val HEX_2 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 25 0b 02 66 66 a2
42 b8 16 46 18"""

        const val HEX_3 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 23 00 0b 02 33 33 ba
42 1d 16 47 21"""

        const val HEX_4 = """fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16 
fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16"""

        val HEX_5 = arrayOf("""
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 14 00 0b 02 00 00 89
43 c0 16 47 23
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 16 00 0b 02 9a 19 89
43 0b 16 47 19
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 15 00 0b 02 9a 19 89
43 0c 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 12 00 0b 02 00 00 89
43 c2 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 11 00 0b 02 33 f3 88
43 9e 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 13 00 0b 02 00 00 89
43 c1 16 47 23
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 08 00 0b 02 9a 59 88
43 da 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 09 00 0b 02 9a 59 88
43 d9 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 10 00 0b 02 33 f3 88
43 9f 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 07 00 0b 02 33 f3 87
43 a9 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 06 00 0b 02 33 f3 87
43 aa 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 05 00 0b 02 66 66 86
43 06 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 04 00 0b 02 33 b3 7d
43 f6 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 03 00 0b 02 9a 19 7a
43 2d 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 02 00 0b 02 9a 99 78
43 b0 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 01 00 0b 02 00 00 78
43 e4 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 12 00 00 0b 02 00 80 77
43 66 16 47 17""",
                """
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 23 00 0b 02 00 80 73
43 48 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 22 00 0b 02 9a 19 72
43 17 16 47 19
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 21 00 0b 02 9a 99 68
43 a2 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 20 00 0b 02 66 66 63
43 0f 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 19 00 0b 02 9a 19 5d
43 35 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 18 00 0b 02 00 80 57
43 6f 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 17 00 0b 02 9a 99 49
43 cb 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 16 00 0b 02 cd cc 48
43 67 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 15 00 0b 02 cd cc 48
43 68 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 14 00 0b 02 33 b3 42
43 22 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 13 00 0b 02 cd 4c 31
43 01 16 47 19
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 12 00 0b 02 cd cc 22
43 91 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 11 00 0b 02 9a 99 22
43 f8 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 10 00 0b 02 cd 4c 15
43 20 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 09 00 0b 02 33 33 0c
43 e3 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 08 00 0b 02 33 33 0c
43 e4 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 07 00 0b 02 cd 4c 09
43 35 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 06 00 0b 02 00 00 fb
42 5e 16 47 16
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 05 00 0b 02 cd cc ec
42 d5 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 04 00 0b 02 66 66 e2
42 ad 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 03 00 0b 02 00 00 d9
42 83 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 02 00 0b 02 cd cc c7
42 fd 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 01 00 0b 02 cd cc c0
42 05 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 11 00 00 0b 02 9a 99 bb
42 71 16 47 17""",
                """
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 23 00 0b 02 33 33 ba
42 1d 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 22 00 0b 02 00 00 b9
42 85 16 47 17
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 21 00 0b 02 00 00 b9
42 86 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 20 00 0b 02 cd cc a5
42 02 16 47 23
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 19 00 0b 02 cd cc a2
42 0c 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 18 00 0b 02 cd cc a2
42 0d 16 47 18
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 17 00 0b 02 cd cc a2
42 0e 16 47 21
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 16 00 0b 02 cd cc a2
42 0f 16 47 20
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 45 0b 02 9a 99 a2
42 31 16 47 19
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 40 0b 02 9a 99 a2
42 36 16 47 19
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 35 0b 02 9a 99 a2
42 41 16 47 18
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 30 0b 02 9a 99 a2
42 46 16 47 23
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 25 0b 02 66 66 a2
42 b8 16 46 18
49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 20 0b 02 00 00 80
3f ae 16 45 20
""")

        val HEX_6 = arrayOf("49 45 53 44 31 30 71 90 39 94 fe fe 68 71",
                "90 39 94 1d 0e 0b 02 aa 19 10 12 14 00 0b 02 00 00 89 43 c0 16 47 23")
    }
}